import type { App, Directive, DirectiveBinding } from 'vue'
// 按钮权限
const auth: Directive = {
  mounted(el: HTMLElement, binding: DirectiveBinding) {
    const { value } = binding
    const currentPageRoles: string[] = JSON.parse(
      sessionStorage.getItem('userPermissions') as string
    )
    if (currentPageRoles && currentPageRoles[0] === '*:*:*') return
    if (value instanceof Array && value.length) {
      const hasPermission = value.every((item) => currentPageRoles.includes(item))
      if (!hasPermission) el.remove()
    } else {
      if (!currentPageRoles.includes(value)) el.remove()
    }
  }
}

const directivesList: any = {
  auth
}
const directives = {
  install: function (app: App<Element>) {
    Object.keys(directivesList).forEach((key) => {
      // 注册所有自定义指令
      app.directive(key, directivesList[key])
    })
  }
}

export default directives
